home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 August: Tool Chest / Apple_Developer_CD_Series_August_1997_Tool_Chest.iso / Sample Code / SetDeskCPatDemo / SetDeskCPat.c < prev    next >
Encoding:
Text File  |  1997-06-19  |  3.1 KB  |  176 lines  |  [TEXT/CWIE]

  1.     //
  2.     //    You may incorporate this sample code into your
  3.     //    applications without restriction. This sample code has
  4.     //    been provided "AS IS" and the responsibility for its
  5.     //    operation is 100% yours. You are not permitted to
  6.     //    redistribute the source as "Apple sample code" after
  7.     //    having made changes. If you're going to re-distribute
  8.     //    the source, we require that you make it clear in the
  9.     //    source that the code was descended from Apple sample
  10.     //    code, but that you've made changes.
  11.     //
  12.  
  13. #define OLDROUTINELOCATIONS            0
  14. #define OLDROUTINENAMES                0
  15. #define SystemSevenOrLater            1
  16.  
  17. #ifndef __FONTS__
  18. #    include <Fonts.h>
  19. #endif
  20.  
  21. #ifndef __ERRORS__
  22. #    include <Errors.h>
  23. #endif
  24.  
  25. #ifndef __RESOURCES__
  26. #    include <Resources.h>
  27. #endif
  28.  
  29. #include "MoveableModalDialog.h"
  30.  
  31. enum
  32. {
  33.     kDialogItem_Button_None,
  34.     kDialogItem_Button_Quit,
  35.     kDialogItem_Button_RGB,
  36.     kDialogItem_Button_Resource,
  37.     kDialogItem_Button_NIL
  38. };
  39.  
  40. static pascal OSErr InitMac (void)
  41. {
  42.     MaxApplZone ( );
  43.     InitGraf (&(qd.thePort));
  44.     InitFonts ( );
  45.     InitWindows ( );
  46.     InitMenus ( );
  47.     TEInit ( );
  48.     InitDialogs (nil);
  49.  
  50.     return noErr;
  51. }
  52.  
  53. static pascal OSErr myMakeRGBPat
  54.     (const RGBColor *color, Boolean inSysHeap, PixPatHandle *pphp)
  55. {
  56.     OSErr err = noErr;
  57.  
  58.     THz savedZone;
  59.  
  60.     if (inSysHeap)
  61.     {
  62.         savedZone = GetZone ( );
  63.         SetZone (SystemZone ( ));
  64.     }
  65.  
  66.     if (!(*pphp = NewPixPat ( )))
  67.         err = QDError ( );
  68.     else
  69.     {
  70.         MakeRGBPat (*pphp,color);
  71.         err = QDError ( );
  72.         if (err)
  73.         {
  74.             DisposePixPat (*pphp);
  75.             *pphp = nil;
  76.         }
  77.     }
  78.  
  79.     if (inSysHeap)
  80.         SetZone (savedZone);
  81.  
  82.     return err;
  83. }
  84.  
  85. static pascal OSErr myGetPixPat (short resID, Boolean inSysHeap, PixPatHandle *pphp)
  86. {
  87.     OSErr err = noErr;
  88.  
  89.     Handle pixPatResH = Get1Resource ('ppat',resID);
  90.     if (!(err = ResError ( )))
  91.     {
  92.         if (!pixPatResH)
  93.             err = resNotFound;
  94.         else
  95.         {
  96.             THz savedZone;
  97.  
  98.             if (inSysHeap)
  99.             {
  100.                 savedZone = GetZone ( );
  101.                 SetZone (SystemZone ( ));
  102.             }
  103.  
  104.             *pphp = GetPixPat (resID);
  105.             if (!*pphp) err = QDError ( );
  106.  
  107.             if (inSysHeap)
  108.                 SetZone (savedZone);
  109.  
  110.             ReleaseResource (pixPatResH);
  111.             if (!err) err = ResError ( );
  112.         }
  113.     }
  114.  
  115.     return err;
  116. }
  117.  
  118. static pascal OSErr SetDeskCPatFromRGB (void)
  119. {
  120.     PixPatHandle pph = nil;
  121.     RGBColor rgbGray = { 0x7FFF, 0x7FFF, 0x7FFF };
  122.     OSErr err = myMakeRGBPat (&rgbGray,true,&pph);
  123.     if (!err) SetDeskCPat (pph);
  124.     return err;
  125. }
  126.  
  127. static pascal OSErr SetDeskCPatFromResource (void)
  128. {
  129.     PixPatHandle pph = nil;
  130.     OSErr err = myGetPixPat (128,true,&pph);
  131.     if (!err) SetDeskCPat (pph);
  132.     return err;
  133. }
  134.  
  135. void main (void)
  136. {
  137.     if (InitMac ( ))
  138.         SysBeep (10);
  139.     else
  140.     {
  141.         DialogRef dlgRef = GetNewDialog (128,nil,(WindowRef)-1);
  142.         if (dlgRef)
  143.         {
  144.             short itemHit;
  145.  
  146.             SetDialogDefaultItem (dlgRef,kDialogItem_Button_Quit);
  147.  
  148.             do
  149.             {
  150.                 MoveableModalDialog (StdFilterProc,&itemHit);
  151.  
  152.                 switch (itemHit)
  153.                 {
  154.                     case kDialogItem_Button_RGB            :
  155.  
  156.                         (void) SetDeskCPatFromRGB ( );
  157.                         break;
  158.  
  159.                     case kDialogItem_Button_Resource    :
  160.  
  161.                         (void) SetDeskCPatFromResource ( );
  162.                         break;
  163.  
  164.                     case kDialogItem_Button_NIL            :
  165.  
  166.                         SetDeskCPat (nil);
  167.                         break;
  168.                 }
  169.             }
  170.             while (itemHit != kDialogItem_Button_Quit);
  171.  
  172.             DisposeDialog (dlgRef);
  173.         }
  174.     }
  175. }
  176.